package com.chw.controller;

import com.chw.common.ChwTS;
import com.chw.model.Commerce;
import com.chw.model.vo.UserVo;
import com.chw.service.CommerceService;
import com.chw.utils.FileUploadUtils;
import com.chw.utils.JWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping("/com")
//解决跨域问题
//@CrossOrigin
public class CommerceController {
    @Autowired
    private CommerceService commerceService;
    @GetMapping("/test")
    public String test(){
        return "this is  王立奥";
    }
    @RequestMapping ("/upload")
    public ChwTS upload(MultipartFile img) throws Exception{
        try{
            if (img==null){
                return ChwTS.error(400,"缺少参数");

            }
            String imgName= img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf("."));
            return  ChwTS.success( FileUploadUtils.uploadFileOss(img.getInputStream(),"imgs/"+ UUID.randomUUID().toString()+imgName));
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }

    }
    @RequestMapping("addUser")
    public  ChwTS addUser(Commerce commerce){
        try{
            if (commerce.getName()==null&&commerce.getPassword()==null){
                return ChwTS.error(400,"请传递参数");
            }

            return ChwTS.success(commerceService.addUser(commerce));
        }catch (Exception exception){
            return ChwTS.error(500,exception.getMessage());
        }
    }


    //登录
    @RequestMapping("/login")
    public ChwTS login(String name,String password){
        try {
            if (name==null&& password==null){
                return ChwTS.error(400,"缺少参数");

            }

            Integer yz= (Integer) commerceService.login(name,password);
            Map map=new HashMap();

            if (yz==0){
                Map login_user=new HashMap();
                login_user.put("name",name);
                //生成token
                String sign= JWT.sign(login_user,60*60*24);
                map.put("code",1);
                map.put("token",sign);

            }else {
                map.put("code",2);
            }
            return ChwTS.success(map);
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }
    }
    //三级联动
    @GetMapping("/area")
    public ChwTS area(){
        try{

            return ChwTS.success(commerceService.area());
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }
    }
    //分页查询
    @RequestMapping("queryUser")
    public ChwTS queryUser(UserVo userVo){
        try {

            return ChwTS.success(commerceService.queryUser(userVo));
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }
    }
    //回显
    @RequestMapping("queryUserById")
    public ChwTS queryUserById(Integer id){
        try {
            if (id==null){
                return   ChwTS.error(400,"缺少参数");
            }
            return ChwTS.success(commerceService.queryUserById(id));
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }
    }
    //修改
    @RequestMapping("updateUser")
    public ChwTS updateUser(Commerce commerce){
        try {
            if (commerce.getId()==null){
                return   ChwTS.error(400,"缺少参数");
            }
            commerceService.updateUser(commerce);
            return ChwTS.success("");
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }
    }
    //删除
    @RequestMapping("deleteUser")
    public ChwTS deleteUser(Integer[] id){
        try {
            if (id==null){
                return   ChwTS.error(400,"缺少参数");
            }
            commerceService.deleteUser(id);
            return ChwTS.success("");
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }
    }
    //根据名称查询
    @RequestMapping("queryByName")

    public ChwTS queryByName(String name) {
        try {
            if (name==null){
                return   ChwTS.error(400,"缺少参数");
            }
            return ChwTS.success(commerceService.queryByName(name));
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }

    }
    //根据账号修改密码
    @RequestMapping("updatePass")
    public ChwTS updatePass(String name,String password) {
        try {
            if (name==null||password==null){
                return  ChwTS.error(400,"缺少参数");
            }


            return ChwTS.success(commerceService.updatePass(name,password));
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }

    }
    //修改生日图片邮箱
    @RequestMapping("updateXinXi")
    public ChwTS updateXinXi(Commerce commerce) {
        try {
            if (commerce.getName()==null){
                return  ChwTS.error(400,"缺少参数");
            }

            return ChwTS.success(commerceService.updateXinXi(commerce));
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }

    }
    //修改生日图片邮箱
    @RequestMapping("updateGeRXinXi")
    public ChwTS updateGeRXinXi(Commerce commerce) {
        try {
            if (commerce.getName()==null){
                return  ChwTS.error(400,"缺少参数");
            }
            commerceService.updateGeRXinXi(commerce);
            return ChwTS.success("");
        }catch (Exception e){
            return ChwTS.error(500,e.getMessage());
        }

    }

}
